package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public void saveItemCat(ItemCat itemCat) {
        /*Date date = new Date();
        itemCat.setStatus(true).setCreated(date).setUpdated(date);*/
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    public void deleteItemCat(Integer id, Integer level) {
        if (level == 3){
            itemCatMapper.deleteById(id);
        }

        if (level == 2){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id)
                        .or()
                        .eq("id",id);
            itemCatMapper.delete(queryWrapper);
        }

        if (level == 1){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            List twoIds = itemCatMapper.selectObjs(queryWrapper);
            queryWrapper.clear();
            queryWrapper.in(twoIds.size()>0,"parent_id",twoIds)
                        .or()
                        .in(twoIds.size()>0,"id",twoIds)
                        .or()
                        .eq("id",id);
            itemCatMapper.delete(queryWrapper);
        }
    }

    @Override
    public void updateStatus(ItemCat itemCat) {
//        itemCat.setUpdated(new Date());
        itemCatMapper.updateById(itemCat);
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        ItemCat temp = new ItemCat();
        /*temp.setId(itemCat.getId())
                .setName(itemCat.getName())
                .setUpdated(new Date());*/
        temp.setId(itemCat.getId())
                .setName(itemCat.getName());
        itemCatMapper.updateById(temp);
    }

    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        for (ItemCat itemCat : itemCatList) {
            Integer key = itemCat.getParentId();
            if (map.containsKey(key)) {
                map.get(key).add(itemCat);
            } else {
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(key, list);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        Map<Integer, List<ItemCat>> map = getMap();
        //根据level获取子级信息
        if (level == 1) { //只获取一级列表信息
            return map.get(0);
        }
        if (level == 2) { //获取一级和二级数据
            return getTwoList(map);
        }
        List<ItemCat> oneList = getThreeList(map);
        long endTime = System.currentTimeMillis();
        System.out.println("优化前的耗时: 500ms,优化后耗时:" + (endTime - startTime) + "ms");
        return oneList;
    }

    //获取三级列表信息  先获取1级数据,再获取2级数据.再获取3级数据

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        //1.调用2级菜单方法.
        List<ItemCat> oneList = getTwoList(map);
        //2.实现思路 遍历一级集合,获取二级数据. 封装三级菜单
        for (ItemCat oneItemCat : oneList) {
            //2.1 获取二级数据
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0) {
                //判断二级集合是否为null.如果为null,表示没有二级菜单.
                continue;
            }
            for (ItemCat twoItemCat : twoList) {
                int twoId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(twoId);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    //通过map集合 获取一级二级菜单信息.
    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        //获取二级信息,应该先遍历一级集合
        for (ItemCat oneItemCat : oneList) {
            int oneId = oneItemCat.getId();
            //根据一级Id,获取二级集合
            List<ItemCat> twoList = map.get(oneId);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }


   /* @Override
    public List<ItemCat> findItemCatList(Integer level) {
        //查询一级菜单
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        //2.查询二级菜单 二级数据是一级数据的子级 封装到一级数据中.
        for (ItemCat oneItemCat : oneList) {
            Integer oneId = oneItemCat.getId();
            //清空原始条件   必须有
            queryWrapper.clear();
            queryWrapper.eq("parent_id",oneId);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat twoItemCat : twoList) {
                //获取二级分类
                int twoId = twoItemCat.getId();
                //查询三级列表
                queryWrapper.clear();
                queryWrapper.eq("parent_id",twoId);
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                //将三级列表  封装到二级对象中
                twoItemCat.setChildren(threeList);
            }
            //将二级对象封装到以及对象中
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }*/
}
